<?php
namespace Cyfy\Modules\Users\Forms;

use \Cyfy\Message;
use \Cyfy\Modules\Forms;
use \Cyfy\Modules\Users\Current;
use \Cyfy\Modules\Users\Groups;


/**
 * Create group form.
 *
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class CreateGroup extends Forms\Form
{
    function structure()
    {
        $this -> permission("User", "groups and permissions");

        $this -> onSubmit = "Group.reload();";
        $i = new Forms\TextBox("name", self :: t(720));
        $i -> required = true;
        $i -> maxLength = 50;
        $this -> addItem($i);
        
        $i = new Forms\TextBox("short", self :: t(730));
        $i -> required = true;
        $i -> description = self :: t(740);
        $i -> maxLength = 5;
        $this -> addItem($i);
        
        //colors style;
        $dataStyle = array(0 => "");
        foreach(Groups :: $availableColors as $color => $name)
        {
            //$color = (string) $color;
            $dataStyle[] = 'font-weight:bold; color: #' . $color;
        }
        
        $data = Groups :: $availableColors;
        array_unshift($data, self :: t(7020));
        
        $i = new Forms\Select("color", self :: t(750));
        $i -> required = true;
        $i -> data = $data;
        $i -> dataStyle = $dataStyle;
        $this -> addItem($i);
        
        // create btn
        $b = new Forms\Button(self :: t(790), Forms\Button::OK);
        $this -> addButton($b);

        if($this->name == "EditGroup")
        {
            $b = new Forms\Button(self :: t(7030), Forms\Button::CANCEL);
            $b -> onClick = "Group.reload(); return false;";
            $this -> addButton($b);
        }
    }

    function submit()
    {
        \dibi :: query("INSERT INTO [:cyfy:Users-groups] %v", $this -> getAll());
        \Cyfy\Message :: set(self :: t(701, $this -> get("name")));
        return true;
    }

}
